package com.woniu.liuzhougov.authorityControl.service.impl;

import com.woniu.liuzhougov.authorityControl.dao.UserMapper;
import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.authorityControl.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @Author zhuxinpan
 * @ClassName UserServiceImpl
 * @Description UserService实现类
 * @Date 2019/4/19 15:17
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    /**
     * @Author zhuxinpan
     * @Description 通过用户名查询user中的表
     * @date 2019/4/19 16:35
     * @params [userLoginName]
     * @return com.woniu.liuzhougov.common.pojo.User
     */
    public User getUserByLoginName(String userLoginName) {
        return userMapper.getUserByLoginName(userLoginName);
    }


    //Item 定义
    @Override
    public Integer getUserIdByUserName(String addItemFounder) {
        return userMapper.getUserIdByUserName(addItemFounder);
    }

    /**
     * @return com.woniu.liuzhougov.common.utils.Result
     * @Author zhuxinpan
     * @Description 更新部门
     * @date 2019/4/23 17:14
     * @params [user]
     */
    @Override
    public int updateDepartmentByUserId(User user) {
        try {
            return userMapper.updateDepartmentByUserId(user);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @return int
     * @Author zhuxinpan
     * @Description 更新用户状态
     * @params [user]
     * @date 2019/4/23 23:05
     */
    @Override
    public int updateUserStatusByUserId(User user) {
        return userMapper.updateUserStatusByUserId(user);
    }

    /**
     * @return com.woniu.liuzhougov.authorityControl.pojo.User
     * @Author zhuxinpan
     * @Description
     * @date 2019/4/24 15:40
     * @params [userId]
     */
    @Override
    public User getUserByUserId(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    /**
     * @return com.woniu.liuzhougov.authorityControl.pojo.User
     * @Author zhuxinpan
     * @Description 得到用户详信息
     * @date 2019/4/24 18:03
     * @params [userId]
     */
    @Override
    public User getUserDetailByUserId(Integer userId) {
        return userMapper.getUserDetailByUserId(userId);
    }

    /**
     * @return int
     * @Author zhuxinpan
     * @Description 统计登录名称数量
     * @date 2019/4/25 9:56
     * @params [user]
     */
    @Override
    public int countLoginNameNumber(User user) {
        return userMapper.countLoginNameNumber(user);
    }

    /**
     * @return int
     * @Author zhuxinpan
     * @Description
     * @date 2019/4/25 10:01
     * @params [user]
     */
    @Override
    public int updateUserDetails(User user) {
        return userMapper.updateUserDetailsByUserId(user);
    }

    /**
     * @return java.util.List<com.woniu.liuzhougov.authorityControl.pojo.User>
     * @Author zhuxinpan
     * @Description 根据部门id获得用户
     * @date 2019/4/25 14:11
     * @params [departmentId]
     */
    @Override
    public List<User> listUserByDepartmentId(Integer departmentId) {
        return userMapper.listUserByDepartmentId(departmentId);
    }

    /**
     * @param user
     * @return int
     * @author zhuxinpan
     * @date 2019/4/28 13:01
     * @description 添加用户
     */
    @Override
    public int insertUser(User user) {
        Md5Hash md5Hash = new Md5Hash(user.getUserPass(), null, 3);
        user.setUserPass(md5Hash.toString());
        return userMapper.insertSelective(user);
    }

    /**
     * @param departmentId
     * @return int
     * @author zhuxinpan
     * @date 2019/4/29 0:58
     * @description 根据部门id统计人数
     */
    @Override
    public int countRecordByDepartmentId(Integer departmentId) {
        return userMapper.countRecordByDepartmentId(departmentId);
    }

    @Override
    /**
     * @Author zhuxinpan
     * @Description 展示所有用户
     * @date 2019/4/22 9:40
     * @params [currentPage, pageSize]
     * @return java.util.List<com.woniu.liuzhougov.authorityControl.pojo.User>
     */
    public List<User> listUser(User user, Integer currentPage, Integer pageSize) {
        return userMapper.listUser(user, currentPage, pageSize);
    }

    /**
     * @return int
     * @Author zhuxinpan
     * @Description 更新用户密码
     * @date 2019/4/25 10:01
     * @params [user]
     */
    @Override
    public int updatePassByUserId(User user) {
        Md5Hash md5Hash = new Md5Hash(user.getUserPass(), null, 3);
        user.setUserPass(md5Hash.toString());
        return userMapper.updateByPrimaryKeySelective(user);
    }


}
